Internationalisation of "magic words" such as #redirect
[lhc/web/wiklou.git] / includes / Article.php
1 <?
2 # Class representing a Wikipedia article and history.
3 # See design.doc for an overview.
4
5 # Note: edit user interface and cache support functions have been
6 # moved to separate EditPage and CacheManager classes.
7
8 include_once( "CacheManager.php" );
9
10 class Article {
11 /* private */ var $mContent, $mContentLoaded;
12 /* private */ var $mUser, $mTimestamp, $mUserText;
13 /* private */ var $mCounter, $mComment, $mCountAdjustment;
14 /* private */ var $mMinorEdit, $mRedirectedFrom;
15 /* private */ var $mTouched, $mFileCache;
16
17 function Article() { $this->clear(); }
18
19 /* private */ function clear()
20 {
21 $this->mContentLoaded = false;
22 $this->mUser = $this->mCounter = -1; # Not loaded
23 $this->mRedirectedFrom = $this->mUserText =
24 $this->mTimestamp = $this->mComment = $this->mFileCache = "";
25 $this->mCountAdjustment = 0;
26 $this->mTouched = "19700101000000";
27 }
28
29 /* static */ function newFromID( $newid )
30 {
31 global $wgOut, $wgTitle, $wgArticle;
32 $a = new Article();
33 $n = Article::nameOf( $newid );
34
35 $wgTitle = Title::newFromDBkey( $n );
36 $wgTitle->resetArticleID( $newid );
37
38 return $a;
39 }
40
41 /* static */ function nameOf( $id )
42 {
43 $sql = "SELECT cur_namespace,cur_title FROM cur WHERE " .
44 "cur_id={$id}";
45 $res = wfQuery( $sql, "Article::nameOf" );
46 if ( 0 == wfNumRows( $res ) ) { return NULL; }
47
48 $s = wfFetchObject( $res );
49 $n = Title::makeName( $s->cur_namespace, $s->cur_title );
50 return $n;
51 }
52
53 # Note that getContent/loadContent may follow redirects if
54 # not told otherwise, and so may cause a change to wgTitle.
55
56 function getContent( $noredir = false )
57 {
58 global $action,$section,$count,$wgTitle; # From query string
59 wfProfileIn( "Article::getContent" );
60
61 if ( 0 == $this->getID() ) {
62 if ( "edit" == $action ) {
63
64 global $wgTitle;
65 return ""; # was "newarticletext", now moved above the box)
66
67
68 }
69 wfProfileOut();
70 return wfMsg( "noarticletext" );
71 } else {
72 $this->loadContent( $noredir );
73 wfProfileOut();
74
75 if(
76 # check if we're displaying a [[User talk:x.x.x.x]] anonymous talk page
77 ( $wgTitle->getNamespace() == Namespace::getTalk( Namespace::getUser()) ) &&
78 preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$wgTitle->getText()) &&
79 $action=="view"
80 )
81 {
82 return $this->mContent . "\n" .wfMsg("anontalkpagetext"); }
83 else {
84 if($action=="edit") {
85 if($section!="") {
86 if($section=="new") { return ""; }
87
88 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
89 $this->mContent, -1,
90 PREG_SPLIT_DELIM_CAPTURE);
91 if($section==0) {
92 return trim($secs[0]);
93 } else {
94 return trim($secs[$section*2-1] . $secs[$section*2]);
95 }
96 }
97 }
98 return $this->mContent;
99 }
100 }
101 }
102
103 function loadContent( $noredir = false )
104 {
105 global $wgOut, $wgTitle, $wgMwRedir;
106 global $oldid, $redirect; # From query
107
108 if ( $this->mContentLoaded ) return;
109 $fname = "Article::loadContent";
110
111 # Pre-fill content with error message so that if something
112 # fails we'll have something telling us what we intended.
113
114 $t = $wgTitle->getPrefixedText();
115 if ( $oldid ) { $t .= ",oldid={$oldid}"; }
116 if ( $redirect ) { $t .= ",redirect={$redirect}"; }
117 $this->mContent = str_replace( "$1", $t, wfMsg( "missingarticle" ) );
118
119 if ( ! $oldid ) { # Retrieve current version
120 $id = $this->getID();
121 if ( 0 == $id ) return;
122
123 $sql = "SELECT " .
124 "cur_text,cur_timestamp,cur_user,cur_counter,cur_restrictions,cur_touched " .
125 "FROM cur WHERE cur_id={$id}";
126 $res = wfQuery( $sql, $fname );
127 if ( 0 == wfNumRows( $res ) ) { return; }
128
129 $s = wfFetchObject( $res );
130
131 # If we got a redirect, follow it (unless we've been told
132 # not to by either the function parameter or the query
133 if ( ( "no" != $redirect ) && ( false == $noredir ) &&
134 ( $wgMwRedir->matchStart( $s->cur_text ) ) ) {
135 if ( preg_match( "/\\[\\[([^\\]\\|]+)[\\]\\|]/",
136 $s->cur_text, $m ) ) {
137 $rt = Title::newFromText( $m[1] );
138
139 # Gotta hand redirects to special pages differently:
140 # Fill the HTTP response "Location" header and ignore
141 # the rest of the page we're on.
142
143 if ( $rt->getInterwiki() != "" ) {
144 $wgOut->redirect( $rt->getFullURL() ) ;
145 return;
146 }
147 if ( $rt->getNamespace() == Namespace::getSpecial() ) {
148 $wgOut->redirect( wfLocalUrl(
149 $rt->getPrefixedURL() ) );
150 return;
151 }
152 $rid = $rt->getArticleID();
153 if ( 0 != $rid ) {
154 $sql = "SELECT cur_text,cur_timestamp,cur_user," .
155 "cur_counter,cur_touched FROM cur WHERE cur_id={$rid}";
156 $res = wfQuery( $sql, $fname );
157
158 if ( 0 != wfNumRows( $res ) ) {
159 $this->mRedirectedFrom = $wgTitle->getPrefixedText();
160 $wgTitle = $rt;
161 $s = wfFetchObject( $res );
162 }
163 }
164 }
165 }
166 $this->mContent = $s->cur_text;
167 $this->mUser = $s->cur_user;
168 $this->mCounter = $s->cur_counter;
169 $this->mTimestamp = $s->cur_timestamp;
170 $this->mTouched = $s->cur_touched;
171 $wgTitle->mRestrictions = explode( ",", trim( $s->cur_restrictions ) );
172 $wgTitle->mRestrictionsLoaded = true;
173 wfFreeResult( $res );
174 } else { # oldid set, retrieve historical version
175 $sql = "SELECT old_text,old_timestamp,old_user FROM old " .
176 "WHERE old_id={$oldid}";
177 $res = wfQuery( $sql, $fname );
178 if ( 0 == wfNumRows( $res ) ) { return; }
179
180 $s = wfFetchObject( $res );
181 $this->mContent = $s->old_text;
182 $this->mUser = $s->old_user;
183 $this->mCounter = 0;
184 $this->mTimestamp = $s->old_timestamp;
185 wfFreeResult( $res );
186 }
187 $this->mContentLoaded = true;
188 }
189
190 function getID() { global $wgTitle; return $wgTitle->getArticleID(); }
191
192 function getCount()
193 {
194 if ( -1 == $this->mCounter ) {
195 $id = $this->getID();
196 $this->mCounter = wfGetSQL( "cur", "cur_counter", "cur_id={$id}" );
197 }
198 return $this->mCounter;
199 }
200
201 # Would the given text make this article a "good" article (i.e.,
202 # suitable for including in the article count)?
203
204 function isCountable( $text )
205 {
206 global $wgTitle, $wgUseCommaCount, $wgMwRedir;
207
208 if ( 0 != $wgTitle->getNamespace() ) { return 0; }
209 if ( $wgMwRedir->matchStart( $text ) ) { return 0; }
210 $token = ($wgUseCommaCount ? "," : "[[" );
211 if ( false === strstr( $text, $token ) ) { return 0; }
212 return 1;
213 }
214
215 # Load the field related to the last edit time of the article.
216 # This isn't necessary for all uses, so it's only done if needed.
217
218 /* private */ function loadLastEdit()
219 {
220 global $wgOut;
221 if ( -1 != $this->mUser ) return;
222
223 $sql = "SELECT cur_user,cur_user_text,cur_timestamp," .
224 "cur_comment,cur_minor_edit FROM cur WHERE " .
225 "cur_id=" . $this->getID();
226 $res = wfQuery( $sql, "Article::loadLastEdit" );
227
228 if ( wfNumRows( $res ) > 0 ) {
229 $s = wfFetchObject( $res );
230 $this->mUser = $s->cur_user;
231 $this->mUserText = $s->cur_user_text;
232 $this->mTimestamp = $s->cur_timestamp;
233 $this->mComment = $s->cur_comment;
234 $this->mMinorEdit = $s->cur_minor_edit;
235 }
236 }
237
238 function getTimestamp()
239 {
240 $this->loadLastEdit();
241 return $this->mTimestamp;
242 }
243
244 function getUser()
245 {
246 $this->loadLastEdit();
247 return $this->mUser;
248 }
249
250 function getUserText()
251 {
252 $this->loadLastEdit();
253 return $this->mUserText;
254 }
255
256 function getComment()
257 {
258 $this->loadLastEdit();
259 return $this->mComment;
260 }
261
262 function getMinorEdit()
263 {
264 $this->loadLastEdit();
265 return $this->mMinorEdit;
266 }
267
268 # This is the default action of the script: just view the page of
269 # the given title.
270
271 function view()
272 {
273 global $wgUser, $wgOut, $wgTitle, $wgLang;
274 global $oldid, $diff; # From query
275 global $wgLinkCache;
276 wfProfileIn( "Article::view" );
277
278 $wgOut->setArticleFlag( true );
279 $wgOut->setRobotpolicy( "index,follow" );
280
281 # If we got diff and oldid in the query, we want to see a
282 # diff page instead of the article.
283
284 if ( isset( $diff ) ) {
285 $wgOut->setPageTitle( $wgTitle->getPrefixedText() );
286 $de = new DifferenceEngine( $oldid, $diff );
287 $de->showDiffPage();
288 wfProfileOut();
289 return;
290 }
291 $text = $this->getContent(); # May change wgTitle!
292 $wgOut->setPageTitle( $wgTitle->getPrefixedText() );
293 $wgOut->setHTMLTitle( $wgTitle->getPrefixedText() .
294 " - " . wfMsg( "wikititlesuffix" ) );
295
296 # We're looking at an old revision
297
298 if ( $oldid ) {
299 $this->setOldSubtitle();
300 $wgOut->setRobotpolicy( "noindex,follow" );
301 }
302 if ( "" != $this->mRedirectedFrom ) {
303 $sk = $wgUser->getSkin();
304 $redir = $sk->makeKnownLink( $this->mRedirectedFrom, "",
305 "redirect=no" );
306 $s = str_replace( "$1", $redir, wfMsg( "redirectedfrom" ) );
307 $wgOut->setSubtitle( $s );
308 }
309 $wgOut->checkLastModified( $this->mTouched );
310 $this->tryFileCache();
311 $wgLinkCache->preFill( $wgTitle );
312 $wgOut->addWikiText( $text );
313
314 # If the article we've just shown is in the "Image" namespace,
315 # follow it with the history list and link list for the image
316 # it describes.
317
318 if ( Namespace::getImage() == $wgTitle->getNamespace() ) {
319 $this->imageHistory();
320 $this->imageLinks();
321 }
322 $this->viewUpdates();
323 wfProfileOut();
324 }
325
326 # Theoretically we could defer these whole insert and update
327 # functions for after display, but that's taking a big leap
328 # of faith, and we want to be able to report database
329 # errors at some point.
330
331 /* private */ function insertNewArticle( $text, $summary, $isminor, $watchthis )
332 {
333 global $wgOut, $wgUser, $wgTitle, $wgLinkCache, $wgMwRedir;
334 $fname = "Article::insertNewArticle";
335
336 $ns = $wgTitle->getNamespace();
337 $ttl = $wgTitle->getDBkey();
338 $text = $this->preSaveTransform( $text );
339 if ( $wgMwRedir->matchStart( $text ) ) { $redir = 1; }
340 else { $redir = 0; }
341
342 $now = wfTimestampNow();
343 $won = wfInvertTimestamp( $now );
344 wfSeedRandom();
345 $rand = number_format( mt_rand() / mt_getrandmax(), 12, ".", "" );
346 $sql = "INSERT INTO cur (cur_namespace,cur_title,cur_text," .
347 "cur_comment,cur_user,cur_timestamp,cur_minor_edit,cur_counter," .
348 "cur_restrictions,cur_user_text,cur_is_redirect," .
349 "cur_is_new,cur_random,cur_touched,inverse_timestamp) VALUES ({$ns},'" . wfStrencode( $ttl ) . "', '" .
350 wfStrencode( $text ) . "', '" .
351 wfStrencode( $summary ) . "', '" .
352 $wgUser->getID() . "', '{$now}', " .
353 ( $isminor ? 1 : 0 ) . ", 0, '', '" .
354 wfStrencode( $wgUser->getName() ) . "', $redir, 1, $rand, '{$now}', '{$won}')";
355 $res = wfQuery( $sql, $fname );
356
357 $newid = wfInsertId();
358 $wgTitle->resetArticleID( $newid );
359
360 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
361 "rc_namespace,rc_title,rc_new,rc_minor,rc_cur_id,rc_user," .
362 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid,rc_bot) VALUES (" .
363 "'{$now}','{$now}',{$ns},'" . wfStrencode( $ttl ) . "',1," .
364 ( $isminor ? 1 : 0 ) . ",{$newid}," . $wgUser->getID() . ",'" .
365 wfStrencode( $wgUser->getName() ) . "','" .
366 wfStrencode( $summary ) . "',0,0," .
367 ( $wgUser->isBot() ? 1 : 0 ) . ")";
368 wfQuery( $sql, $fname );
369 if ($watchthis) {
370 if(!$wgTitle->userIsWatching()) $this->watch();
371 } else {
372 if ( $wgTitle->userIsWatching() ) {
373 $this->unwatch();
374 }
375 }
376
377 $this->showArticle( $text, wfMsg( "newarticle" ) );
378 }
379
380 function updateArticle( $text, $summary, $minor, $watchthis, $section="" )
381 {
382 global $wgOut, $wgUser, $wgTitle, $wgLinkCache;
383 global $wgDBtransactions, $wgMwRedir;
384 $fname = "Article::updateArticle";
385
386 $this->loadLastEdit();
387
388 // insert updated section into old text if we have only edited part
389 // of the article
390 if ($section != "") {
391 $oldtext=$this->getContent();
392 if($section=="new") {
393 if($summary) $subject="== {$summary} ==\n\n";
394 $text=$oldtext."\n\n".$subject.$text;
395 } else {
396 $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?>.*?<\/h[1-6].*?>)/mi",
397 $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE);
398 $secs[$section*2]=$text."\n\n"; // replace with edited
399 if($section) { $secs[$section*2-1]=""; } // erase old headline
400 $text=join("",$secs);
401 }
402 }
403 if ( $this->mMinorEdit ) { $me1 = 1; } else { $me1 = 0; }
404 if ( $minor ) { $me2 = 1; } else { $me2 = 0; }
405 if ( preg_match( "/^((" . $wgMwRedir->getBaseRegex() . ")[^\\n]+)/i", $text, $m ) ) {
406 $redir = 1;
407 $text = $m[1] . "\n"; # Remove all content but redirect
408 }
409 else { $redir = 0; }
410
411 $text = $this->preSaveTransform( $text );
412
413 # Update article, but only if changed.
414
415 if( $wgDBtransactions ) {
416 $sql = "BEGIN";
417 wfQuery( $sql );
418 }
419 $oldtext = $this->getContent( true );
420
421 if ( 0 != strcmp( $text, $oldtext ) ) {
422 $this->mCountAdjustment = $this->isCountable( $text )
423 - $this->isCountable( $oldtext );
424
425 $now = wfTimestampNow();
426 $won = wfInvertTimestamp( $now );
427 $sql = "UPDATE cur SET cur_text='" . wfStrencode( $text ) .
428 "',cur_comment='" . wfStrencode( $summary ) .
429 "',cur_minor_edit={$me2}, cur_user=" . $wgUser->getID() .
430 ",cur_timestamp='{$now}',cur_user_text='" .
431 wfStrencode( $wgUser->getName() ) .
432 "',cur_is_redirect={$redir}, cur_is_new=0, cur_touched='{$now}', inverse_timestamp='{$won}' " .
433 "WHERE cur_id=" . $this->getID() .
434 " AND cur_timestamp='" . $this->getTimestamp() . "'";
435 $res = wfQuery( $sql, $fname );
436
437 if( wfAffectedRows() == 0 ) {
438 /* Belated edit conflict! Run away!! */
439 return false;
440 }
441
442 $sql = "INSERT INTO old (old_namespace,old_title,old_text," .
443 "old_comment,old_user,old_user_text,old_timestamp," .
444 "old_minor_edit,inverse_timestamp) VALUES (" .
445 $wgTitle->getNamespace() . ", '" .
446 wfStrencode( $wgTitle->getDBkey() ) . "', '" .
447 wfStrencode( $oldtext ) . "', '" .
448 wfStrencode( $this->getComment() ) . "', " .
449 $this->getUser() . ", '" .
450 wfStrencode( $this->getUserText() ) . "', '" .
451 $this->getTimestamp() . "', " . $me1 . ", '" .
452 wfInvertTimestamp( $this->getTimestamp() ) . "')";
453 $res = wfQuery( $sql, $fname );
454 $oldid = wfInsertID( $res );
455
456 $sql = "INSERT INTO recentchanges (rc_timestamp,rc_cur_time," .
457 "rc_namespace,rc_title,rc_new,rc_minor,rc_bot,rc_cur_id,rc_user," .
458 "rc_user_text,rc_comment,rc_this_oldid,rc_last_oldid) VALUES (" .
459 "'{$now}','{$now}'," . $wgTitle->getNamespace() . ",'" .
460 wfStrencode( $wgTitle->getDBkey() ) . "',0,{$me2}," .
461 ( $wgUser->isBot() ? 1 : 0 ) . "," .
462 $this->getID() . "," . $wgUser->getID() . ",'" .
463 wfStrencode( $wgUser->getName() ) . "','" .
464 wfStrencode( $summary ) . "',0,{$oldid})";
465 wfQuery( $sql, $fname );
466
467 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
468 "WHERE rc_namespace=" . $wgTitle->getNamespace() . " AND " .
469 "rc_title='" . wfStrencode( $wgTitle->getDBkey() ) . "' AND " .
470 "rc_timestamp='" . $this->getTimestamp() . "'";
471 wfQuery( $sql, $fname );
472
473 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
474 "WHERE rc_cur_id=" . $this->getID();
475 wfQuery( $sql, $fname );
476 }
477 if( $wgDBtransactions ) {
478 $sql = "COMMIT";
479 wfQuery( $sql );
480 }
481
482 if ($watchthis) {
483 if (!$wgTitle->userIsWatching()) $this->watch();
484 } else {
485 if ( $wgTitle->userIsWatching() ) {
486 $this->unwatch();
487 }
488 }
489
490 $this->showArticle( $text, wfMsg( "updated" ) );
491 return true;
492 }
493
494 # After we've either updated or inserted the article, update
495 # the link tables and redirect to the new page.
496
497 function showArticle( $text, $subtitle )
498 {
499 global $wgOut, $wgTitle, $wgUser, $wgLinkCache, $wgUseBetterLinksUpdate;
500 global $wgMwRedir;
501
502 $wgLinkCache = new LinkCache();
503
504 # Get old version of link table to allow incremental link updates
505 if ( $wgUseBetterLinksUpdate ) {
506 $wgLinkCache->preFill( $wgTitle );
507 $wgLinkCache->clear();
508 }
509
510 # Now update the link cache by parsing the text
511 $wgOut->addWikiText( $text );
512
513 $this->editUpdates( $text );
514 if( $wgMwRedir->matchStart( $text ) )
515 $r = "redirect=no";
516 else
517 $r = "";
518 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL(), $r ) );
519 }
520
521 # If the page we've just displayed is in the "Image" namespace,
522 # we follow it with an upload history of the image and its usage.
523
524 function imageHistory()
525 {
526 global $wgUser, $wgOut, $wgLang, $wgTitle;
527 $fname = "Article::imageHistory";
528
529 $sql = "SELECT img_size,img_description,img_user," .
530 "img_user_text,img_timestamp FROM image WHERE " .
531 "img_name='" . wfStrencode( $wgTitle->getDBkey() ) . "'";
532 $res = wfQuery( $sql, $fname );
533
534 if ( 0 == wfNumRows( $res ) ) { return; }
535
536 $sk = $wgUser->getSkin();
537 $s = $sk->beginImageHistoryList();
538
539 $line = wfFetchObject( $res );
540 $s .= $sk->imageHistoryLine( true, $line->img_timestamp,
541 $wgTitle->getText(), $line->img_user,
542 $line->img_user_text, $line->img_size, $line->img_description );
543
544 $sql = "SELECT oi_size,oi_description,oi_user," .
545 "oi_user_text,oi_timestamp,oi_archive_name FROM oldimage WHERE " .
546 "oi_name='" . wfStrencode( $wgTitle->getDBkey() ) . "' " .
547 "ORDER BY oi_timestamp DESC";
548 $res = wfQuery( $sql, $fname );
549
550 while ( $line = wfFetchObject( $res ) ) {
551 $s .= $sk->imageHistoryLine( false, $line->oi_timestamp,
552 $line->oi_archive_name, $line->oi_user,
553 $line->oi_user_text, $line->oi_size, $line->oi_description );
554 }
555 $s .= $sk->endImageHistoryList();
556 $wgOut->addHTML( $s );
557 }
558
559 function imageLinks()
560 {
561 global $wgUser, $wgOut, $wgTitle;
562
563 $wgOut->addHTML( "<h2>" . wfMsg( "imagelinks" ) . "</h2>\n" );
564
565 $sql = "SELECT il_from FROM imagelinks WHERE il_to='" .
566 wfStrencode( $wgTitle->getDBkey() ) . "'";
567 $res = wfQuery( $sql, "Article::imageLinks" );
568
569 if ( 0 == wfNumRows( $res ) ) {
570 $wgOut->addHtml( "<p>" . wfMsg( "nolinkstoimage" ) . "\n" );
571 return;
572 }
573 $wgOut->addHTML( "<p>" . wfMsg( "linkstoimage" ) . "\n<ul>" );
574
575 $sk = $wgUser->getSkin();
576 while ( $s = wfFetchObject( $res ) ) {
577 $name = $s->il_from;
578 $link = $sk->makeKnownLink( $name, "" );
579 $wgOut->addHTML( "<li>{$link}</li>\n" );
580 }
581 $wgOut->addHTML( "</ul>\n" );
582 }
583
584 # Add this page to my watchlist
585
586 function watch()
587 {
588 global $wgUser, $wgTitle, $wgOut, $wgLang;
589 global $wgDeferredUpdateList;
590
591 if ( 0 == $wgUser->getID() ) {
592 $wgOut->errorpage( "watchnologin", "watchnologintext" );
593 return;
594 }
595 if ( wfReadOnly() ) {
596 $wgOut->readOnlyPage();
597 return;
598 }
599 $wgUser->addWatch( $wgTitle );
600
601 $wgOut->setPagetitle( wfMsg( "addedwatch" ) );
602 $wgOut->setRobotpolicy( "noindex,follow" );
603
604 $sk = $wgUser->getSkin() ;
605 $link = $sk->makeKnownLink ( $wgTitle->getPrefixedText() ) ;
606
607 $text = str_replace( "$1", $link ,
608 wfMsg( "addedwatchtext" ) );
609 $wgOut->addHTML( $text );
610
611 $up = new UserUpdate();
612 array_push( $wgDeferredUpdateList, $up );
613
614 $wgOut->returnToMain( false );
615 }
616
617 function unwatch()
618 {
619 global $wgUser, $wgTitle, $wgOut, $wgLang;
620 global $wgDeferredUpdateList;
621
622 if ( 0 == $wgUser->getID() ) {
623 $wgOut->errorpage( "watchnologin", "watchnologintext" );
624 return;
625 }
626 if ( wfReadOnly() ) {
627 $wgOut->readOnlyPage();
628 return;
629 }
630 $wgUser->removeWatch( $wgTitle );
631
632 $wgOut->setPagetitle( wfMsg( "removedwatch" ) );
633 $wgOut->setRobotpolicy( "noindex,follow" );
634
635 $sk = $wgUser->getSkin() ;
636 $link = $sk->makeKnownLink ( $wgTitle->getPrefixedText() ) ;
637
638 $text = str_replace( "$1", $link ,
639 wfMsg( "removedwatchtext" ) );
640 $wgOut->addHTML( $text );
641
642 $up = new UserUpdate();
643 array_push( $wgDeferredUpdateList, $up );
644
645 $wgOut->returnToMain( false );
646 }
647
648 # This shares a lot of issues (and code) with Recent Changes
649
650 function history()
651 {
652 global $wgUser, $wgOut, $wgLang, $wgTitle, $offset, $limit;
653
654 # If page hasn't changed, client can cache this
655
656 $wgOut->checkLastModified( $this->getTimestamp() );
657 wfProfileIn( "Article::history" );
658
659 $wgOut->setPageTitle( $wgTitle->getPRefixedText() );
660 $wgOut->setSubtitle( wfMsg( "revhistory" ) );
661 $wgOut->setArticleFlag( false );
662 $wgOut->setRobotpolicy( "noindex,nofollow" );
663
664 if( $wgTitle->getArticleID() == 0 ) {
665 $wgOut->addHTML( wfMsg( "nohistory" ) );
666 wfProfileOut();
667 return;
668 }
669
670 $offset = (int)$offset;
671 $limit = (int)$limit;
672 if( $limit == 0 ) $limit = 50;
673 $namespace = $wgTitle->getNamespace();
674 $title = $wgTitle->getText();
675 $sql = "SELECT old_id,old_user," .
676 "old_comment,old_user_text,old_timestamp,old_minor_edit ".
677 "FROM old USE INDEX (name_title_timestamp) " .
678 "WHERE old_namespace={$namespace} AND " .
679 "old_title='" . wfStrencode( $wgTitle->getDBkey() ) . "' " .
680 "ORDER BY inverse_timestamp LIMIT $offset, $limit";
681 $res = wfQuery( $sql, "Article::history" );
682
683 $revs = wfNumRows( $res );
684 if( $wgTitle->getArticleID() == 0 ) {
685 $wgOut->addHTML( wfMsg( "nohistory" ) );
686 wfProfileOut();
687 return;
688 }
689
690 $sk = $wgUser->getSkin();
691 $numbar = wfViewPrevNext(
692 $offset, $limit,
693 $wgTitle->getPrefixedText(),
694 "action=history" );
695 $s = $numbar;
696 $s .= $sk->beginHistoryList();
697
698 if($offset == 0 )
699 $s .= $sk->historyLine( $this->getTimestamp(), $this->getUser(),
700 $this->getUserText(), $namespace,
701 $title, 0, $this->getComment(),
702 ( $this->getMinorEdit() > 0 ) );
703
704 $revs = wfNumRows( $res );
705 while ( $line = wfFetchObject( $res ) ) {
706 $s .= $sk->historyLine( $line->old_timestamp, $line->old_user,
707 $line->old_user_text, $namespace,
708 $title, $line->old_id,
709 $line->old_comment, ( $line->old_minor_edit > 0 ) );
710 }
711 $s .= $sk->endHistoryList();
712 $s .= $numbar;
713 $wgOut->addHTML( $s );
714 wfProfileOut();
715 }
716
717 function protect()
718 {
719 global $wgUser, $wgOut, $wgTitle;
720
721 if ( ! $wgUser->isSysop() ) {
722 $wgOut->sysopRequired();
723 return;
724 }
725 if ( wfReadOnly() ) {
726 $wgOut->readOnlyPage();
727 return;
728 }
729 $id = $wgTitle->getArticleID();
730 if ( 0 == $id ) {
731 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
732 return;
733 }
734 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
735 "cur_restrictions='sysop' WHERE cur_id={$id}";
736 wfQuery( $sql, "Article::protect" );
737
738 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ) );
739 }
740
741 function unprotect()
742 {
743 global $wgUser, $wgOut, $wgTitle;
744
745 if ( ! $wgUser->isSysop() ) {
746 $wgOut->sysopRequired();
747 return;
748 }
749 if ( wfReadOnly() ) {
750 $wgOut->readOnlyPage();
751 return;
752 }
753 $id = $wgTitle->getArticleID();
754 if ( 0 == $id ) {
755 $wgOut->fatalEror( wfMsg( "badarticleerror" ) );
756 return;
757 }
758 $sql = "UPDATE cur SET cur_touched='" . wfTimestampNow() . "'," .
759 "cur_restrictions='' WHERE cur_id={$id}";
760 wfQuery( $sql, "Article::unprotect" );
761
762 $wgOut->redirect( wfLocalUrl( $wgTitle->getPrefixedURL() ) );
763 }
764
765 function delete()
766 {
767 global $wgUser, $wgOut, $wgTitle;
768 global $wpConfirm, $wpReason, $image, $oldimage;
769
770 # Anybody can delete old revisions of images; only sysops
771 # can delete articles and current images
772
773 if ( ( ! $oldimage ) && ( ! $wgUser->isSysop() ) ) {
774 $wgOut->sysopRequired();
775 return;
776 }
777 if ( wfReadOnly() ) {
778 $wgOut->readOnlyPage();
779 return;
780 }
781
782 # Better double-check that it hasn't been deleted yet!
783 $wgOut->setPagetitle( wfMsg( "confirmdelete" ) );
784 if ( $image ) {
785 if ( "" == trim( $image ) ) {
786 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
787 return;
788 }
789 $sub = str_replace( "$1", $image, wfMsg( "deletesub" ) );
790 } else {
791
792 if ( ( "" == trim( $wgTitle->getText() ) )
793 or ( $wgTitle->getArticleId() == 0 ) ) {
794 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
795 return;
796 }
797 $sub = str_replace( "$1", $wgTitle->getPrefixedText(),
798 wfMsg( "deletesub" ) );
799
800 # determine whether this page has earlier revisions
801 # and insert a warning if it does
802 # we select the text because it might be useful below
803 $sql="SELECT old_text FROM old WHERE old_namespace=0 and old_title='" . wfStrencode($wgTitle->getPrefixedDBkey())."' ORDER BY inverse_timestamp LIMIT 1";
804 $res=wfQuery($sql,$fname);
805 if( ($old=wfFetchObject($res)) && !$wpConfirm ) {
806 $skin=$wgUser->getSkin();
807 $wgOut->addHTML("<B>".wfMsg("historywarning"));
808 $wgOut->addHTML( $skin->historyLink() ."</B><P>");
809 }
810
811 $sql="SELECT cur_text FROM cur WHERE cur_namespace=0 and cur_title='" . wfStrencode($wgTitle->getPrefixedDBkey())."'";
812 $res=wfQuery($sql,$fname);
813 if( ($s=wfFetchObject($res))) {
814
815 # if this is a mini-text, we can paste part of it into the deletion reason
816
817 #if this is empty, an earlier revision may contain "useful" text
818 if($s->cur_text!="") {
819 $text=$s->cur_text;
820 } else {
821 if($old) {
822 $text=$old->old_text;
823 $blanked=1;
824 }
825
826 }
827
828 $length=strlen($text);
829
830 # this should not happen, since it is not possible to store an empty, new
831 # page. Let's insert a standard text in case it does, though
832 if($length==0 && !$wpReason) { $wpReason=wfmsg("exblank");}
833
834
835 if($length < 500 && !$wpReason) {
836
837 # comment field=255, let's grep the first 150 to have some user
838 # space left
839 $text=substr($text,0,150);
840 # let's strip out newlines and HTML tags
841 $text=preg_replace("/\"/","'",$text);
842 $text=preg_replace("/\</","&lt;",$text);
843 $text=preg_replace("/\>/","&gt;",$text);
844 $text=preg_replace("/[\n\r]/","",$text);
845 if(!$blanked) {
846 $wpReason=wfMsg("excontent"). " '".$text;
847 } else {
848 $wpReason=wfMsg("exbeforeblank") . " '".$text;
849 }
850 if($length>150) { $wpReason .= "..."; } # we've only pasted part of the text
851 $wpReason.="'";
852 }
853 }
854
855 }
856
857 # Likewise, deleting old images doesn't require confirmation
858 if ( $oldimage || 1 == $wpConfirm ) {
859 $this->doDelete();
860 return;
861 }
862
863 $wgOut->setSubtitle( $sub );
864 $wgOut->setRobotpolicy( "noindex,nofollow" );
865 $wgOut->addWikiText( wfMsg( "confirmdeletetext" ) );
866
867 $t = $wgTitle->getPrefixedURL();
868 $q = "action=delete";
869
870 if ( $image ) {
871 $q .= "&image={$image}";
872 } else if ( $oldimage ) {
873 $q .= "&oldimage={$oldimage}";
874 } else {
875 $q .= "&title={$t}";
876 }
877 $formaction = wfEscapeHTML( wfLocalUrl( "", $q ) );
878 $confirm = wfMsg( "confirm" );
879 $check = wfMsg( "confirmcheck" );
880 $delcom = wfMsg( "deletecomment" );
881
882 $wgOut->addHTML( "
883 <form id=\"deleteconfirm\" method=\"post\" action=\"{$formaction}\">
884 <table border=0><tr><td align=right>
885 {$delcom}:</td><td align=left>
886 <input type=text size=60 name=\"wpReason\" value=\"{$wpReason}\">
887 </td></tr><tr><td>&nbsp;</td></tr>
888 <tr><td align=right>
889 <input type=checkbox name=\"wpConfirm\" value='1' id=\"wpConfirm\">
890 </td><td><label for=\"wpConfirm\">{$check}</label></td>
891 </tr><tr><td>&nbsp;</td><td>
892 <input type=submit name=\"wpConfirmB\" value=\"{$confirm}\">
893 </td></tr></table></form>\n" );
894
895 $wgOut->returnToMain( false );
896 }
897
898 function doDelete()
899 {
900 global $wgOut, $wgTitle, $wgUser, $wgLang;
901 global $image, $oldimage, $wpReason;
902 $fname = "Article::doDelete";
903
904 if ( $image ) {
905 $dest = wfImageDir( $image );
906 $archive = wfImageDir( $image );
907 if ( ! unlink( "{$dest}/{$image}" ) ) {
908 $wgOut->fileDeleteError( "{$dest}/{$image}" );
909 return;
910 }
911 $sql = "DELETE FROM image WHERE img_name='" .
912 wfStrencode( $image ) . "'";
913 wfQuery( $sql, $fname );
914
915 $sql = "SELECT oi_archive_name FROM oldimage WHERE oi_name='" .
916 wfStrencode( $image ) . "'";
917 $res = wfQuery( $sql, $fname );
918
919 while ( $s = wfFetchObject( $res ) ) {
920 $this->doDeleteOldImage( $s->oi_archive_name );
921 }
922 $sql = "DELETE FROM oldimage WHERE oi_name='" .
923 wfStrencode( $image ) . "'";
924 wfQuery( $sql, $fname );
925
926 # Image itself is now gone, and database is cleaned.
927 # Now we remove the image description page.
928
929 $nt = Title::newFromText( $wgLang->getNsText( Namespace::getImage() ) . ":" . $image );
930 $this->doDeleteArticle( $nt );
931
932 $deleted = $image;
933 } else if ( $oldimage ) {
934 $this->doDeleteOldImage( $oldimage );
935 $sql = "DELETE FROM oldimage WHERE oi_archive_name='" .
936 wfStrencode( $oldimage ) . "'";
937 wfQuery( $sql, $fname );
938
939 $deleted = $oldimage;
940 } else {
941 $this->doDeleteArticle( $wgTitle );
942 $deleted = $wgTitle->getPrefixedText();
943 }
944 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
945 $wgOut->setRobotpolicy( "noindex,nofollow" );
946
947 $sk = $wgUser->getSkin();
948 $loglink = $sk->makeKnownLink( $wgLang->getNsText(
949 Namespace::getWikipedia() ) .
950 ":" . wfMsg( "dellogpage" ), wfMsg( "deletionlog" ) );
951
952 $text = str_replace( "$1" , $deleted, wfMsg( "deletedtext" ) );
953 $text = str_replace( "$2", $loglink, $text );
954
955 $wgOut->addHTML( "<p>" . $text );
956 $wgOut->returnToMain( false );
957 }
958
959 function doDeleteOldImage( $oldimage )
960 {
961 global $wgOut;
962
963 $name = substr( $oldimage, 15 );
964 $archive = wfImageArchiveDir( $name );
965 if ( ! unlink( "{$archive}/{$oldimage}" ) ) {
966 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
967 }
968 }
969
970 function doDeleteArticle( $title )
971 {
972 global $wgUser, $wgOut, $wgLang, $wpReason, $wgTitle, $wgDeferredUpdateList;
973
974 $fname = "Article::doDeleteArticle";
975 $ns = $title->getNamespace();
976 $t = wfStrencode( $title->getDBkey() );
977 $id = $title->getArticleID();
978
979 if ( "" == $t ) {
980 $wgOut->fatalError( wfMsg( "cannotdelete" ) );
981 return;
982 }
983
984 $u = new SiteStatsUpdate( 0, 1, -$this->isCountable( $this->getContent( true ) ) );
985 array_push( $wgDeferredUpdateList, $u );
986
987 # Move article and history to the "archive" table
988 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
989 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
990 "ar_flags) SELECT cur_namespace,cur_title,cur_text,cur_comment," .
991 "cur_user,cur_user_text,cur_timestamp,cur_minor_edit,0 FROM cur " .
992 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
993 wfQuery( $sql, $fname );
994
995 $sql = "INSERT INTO archive (ar_namespace,ar_title,ar_text," .
996 "ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit," .
997 "ar_flags) SELECT old_namespace,old_title,old_text,old_comment," .
998 "old_user,old_user_text,old_timestamp,old_minor_edit,old_flags " .
999 "FROM old WHERE old_namespace={$ns} AND old_title='{$t}'";
1000 wfQuery( $sql, $fname );
1001
1002 # Now that it's safely backed up, delete it
1003
1004 $sql = "DELETE FROM cur WHERE cur_namespace={$ns} AND " .
1005 "cur_title='{$t}'";
1006 wfQuery( $sql, $fname );
1007
1008 $sql = "DELETE FROM old WHERE old_namespace={$ns} AND " .
1009 "old_title='{$t}'";
1010 wfQuery( $sql, $fname );
1011
1012 $sql = "DELETE FROM recentchanges WHERE rc_namespace={$ns} AND " .
1013 "rc_title='{$t}'";
1014 wfQuery( $sql, $fname );
1015
1016 # Finally, clean up the link tables
1017
1018 if ( 0 != $id ) {
1019 $t = wfStrencode( $title->getPrefixedDBkey() );
1020 $sql = "SELECT l_from FROM links WHERE l_to={$id}";
1021 $res = wfQuery( $sql, $fname );
1022
1023 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
1024 $now = wfTimestampNow();
1025 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
1026 $first = true;
1027
1028 while ( $s = wfFetchObject( $res ) ) {
1029 $nt = Title::newFromDBkey( $s->l_from );
1030 $lid = $nt->getArticleID();
1031
1032 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
1033 $first = false;
1034 $sql .= "({$lid},'{$t}')";
1035 $sql2 .= "{$lid}";
1036 }
1037 $sql2 .= ")";
1038 if ( ! $first ) {
1039 wfQuery( $sql, $fname );
1040 wfQuery( $sql2, $fname );
1041 }
1042 wfFreeResult( $res );
1043
1044 $sql = "DELETE FROM links WHERE l_to={$id}";
1045 wfQuery( $sql, $fname );
1046
1047 $sql = "DELETE FROM links WHERE l_from='{$t}'";
1048 wfQuery( $sql, $fname );
1049
1050 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
1051 wfQuery( $sql, $fname );
1052
1053 $sql = "DELETE FROM brokenlinks WHERE bl_from={$id}";
1054 wfQuery( $sql, $fname );
1055 }
1056
1057 $log = new LogPage( wfMsg( "dellogpage" ), wfMsg( "dellogpagetext" ) );
1058 $art = $title->getPrefixedText();
1059 $wpReason = wfCleanQueryVar( $wpReason );
1060 $log->addEntry( str_replace( "$1", $art, wfMsg( "deletedarticle" ) ), $wpReason );
1061
1062 # Clear the cached article id so the interface doesn't act like we exist
1063 $wgTitle->resetArticleID( 0 );
1064 $wgTitle->mArticleID = 0;
1065 }
1066
1067 function revert()
1068 {
1069 global $wgOut;
1070 global $oldimage;
1071
1072 if ( strlen( $oldimage ) < 16 ) {
1073 $wgOut->unexpectedValueError( "oldimage", $oldimage );
1074 return;
1075 }
1076 if ( wfReadOnly() ) {
1077 $wgOut->readOnlyPage();
1078 return;
1079 }
1080 $name = substr( $oldimage, 15 );
1081
1082 $dest = wfImageDir( $name );
1083 $archive = wfImageArchiveDir( $name );
1084 $curfile = "{$dest}/{$name}";
1085
1086 if ( ! is_file( $curfile ) ) {
1087 $wgOut->fileNotFoundError( $curfile );
1088 return;
1089 }
1090 $oldver = wfTimestampNow() . "!{$name}";
1091 $size = wfGetSQL( "oldimage", "oi_size", "oi_archive_name='" .
1092 wfStrencode( $oldimage ) . "'" );
1093
1094 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
1095 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
1096 return;
1097 }
1098 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
1099 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
1100 }
1101 wfRecordUpload( $name, $oldver, $size, wfMsg( "reverted" ) );
1102
1103 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1104 $wgOut->setRobotpolicy( "noindex,nofollow" );
1105 $wgOut->addHTML( wfMsg( "imagereverted" ) );
1106 $wgOut->returnToMain( false );
1107 }
1108
1109 function rollback()
1110 {
1111 global $wgUser, $wgTitle, $wgLang, $wgOut, $from;
1112
1113 if ( ! $wgUser->isSysop() ) {
1114 $wgOut->sysopRequired();
1115 return;
1116 }
1117
1118 # Replace all this user's current edits with the next one down
1119 $tt = wfStrencode( $wgTitle->getDBKey() );
1120 $n = $wgTitle->getNamespace();
1121
1122 # Get the last editor
1123 $sql = "SELECT cur_id,cur_user,cur_user_text,cur_comment FROM cur WHERE cur_title='{$tt}' AND cur_namespace={$n}";
1124 $res = wfQuery( $sql );
1125 if( ($x = wfNumRows( $res )) != 1 ) {
1126 # Something wrong
1127 $wgOut->addHTML( wfMsg( "notanarticle" ) );
1128 return;
1129 }
1130 $s = wfFetchObject( $res );
1131 $ut = wfStrencode( $s->cur_user_text );
1132 $uid = $s->cur_user;
1133 $pid = $s->cur_id;
1134
1135 $from = str_replace( '_', ' ', wfCleanQueryVar( $from ) );
1136 if( $from != $s->cur_user_text ) {
1137 $wgOut->setPageTitle(wfmsg("rollbackfailed"));
1138 $wgOut->addWikiText( wfMsg( "alreadyrolled",
1139 htmlspecialchars( $wgTitle->getPrefixedText()),
1140 htmlspecialchars( $from ),
1141 htmlspecialchars( $s->cur_user_text ) ) );
1142 if($s->cur_comment != "") {
1143 $wgOut->addHTML(
1144 wfMsg("editcomment",
1145 htmlspecialchars( $s->cur_comment ) ) );
1146 }
1147 return;
1148 }
1149
1150 # Get the last edit not by this guy
1151 $sql = "SELECT old_text,old_user,old_user_text
1152 FROM old USE INDEX (name_title_timestamp)
1153 WHERE old_namespace={$n} AND old_title='{$tt}'
1154 AND (old_user <> {$uid} OR old_user_text <> '{$ut}')
1155 ORDER BY inverse_timestamp LIMIT 1";
1156 $res = wfQuery( $sql );
1157 if( wfNumRows( $res ) != 1 ) {
1158 # Something wrong
1159 $wgOut->setPageTitle(wfMsg("rollbackfailed"));
1160 $wgOut->addHTML( wfMsg( "cantrollback" ) );
1161 return;
1162 }
1163 $s = wfFetchObject( $res );
1164
1165 # Save it!
1166 $newcomment = str_replace( "$1", $s->old_user_text, wfMsg( "revertpage" ) );
1167 $wgOut->setPagetitle( wfMsg( "actioncomplete" ) );
1168 $wgOut->setRobotpolicy( "noindex,nofollow" );
1169 $wgOut->addHTML( "<h2>" . $newcomment . "</h2>\n<hr>\n" );
1170 $this->updateArticle( $s->old_text, $newcomment, 1, $wgTitle->userIsWatching() );
1171
1172 $wgOut->returnToMain( false );
1173 }
1174
1175
1176 # Do standard deferred updates after page view
1177
1178 /* private */ function viewUpdates()
1179 {
1180 global $wgDeferredUpdateList, $wgTitle;
1181
1182 if ( 0 != $this->getID() ) {
1183 $u = new ViewCountUpdate( $this->getID() );
1184 array_push( $wgDeferredUpdateList, $u );
1185 $u = new SiteStatsUpdate( 1, 0, 0 );
1186 array_push( $wgDeferredUpdateList, $u );
1187
1188 $u = new UserTalkUpdate( 0, $wgTitle->getNamespace(),
1189 $wgTitle->getDBkey() );
1190 array_push( $wgDeferredUpdateList, $u );
1191 }
1192 }
1193
1194 # Do standard deferred updates after page edit.
1195 # Every 1000th edit, prune the recent changes table.
1196
1197 /* private */ function editUpdates( $text )
1198 {
1199 global $wgDeferredUpdateList, $wgTitle;
1200
1201 wfSeedRandom();
1202 if ( 0 == mt_rand( 0, 999 ) ) {
1203 $cutoff = wfUnix2Timestamp( time() - ( 7 * 86400 ) );
1204 $sql = "DELETE FROM recentchanges WHERE rc_timestamp < '{$cutoff}'";
1205 wfQuery( $sql );
1206 }
1207 $id = $this->getID();
1208 $title = $wgTitle->getPrefixedDBkey();
1209 $adj = $this->mCountAdjustment;
1210
1211 if ( 0 != $id ) {
1212 $u = new LinksUpdate( $id, $title );
1213 array_push( $wgDeferredUpdateList, $u );
1214 $u = new SiteStatsUpdate( 0, 1, $adj );
1215 array_push( $wgDeferredUpdateList, $u );
1216 $u = new SearchUpdate( $id, $title, $text );
1217 array_push( $wgDeferredUpdateList, $u );
1218
1219 $u = new UserTalkUpdate( 1, $wgTitle->getNamespace(),
1220 $wgTitle->getDBkey() );
1221 array_push( $wgDeferredUpdateList, $u );
1222 }
1223 }
1224
1225 /* private */ function setOldSubtitle()
1226 {
1227 global $wgLang, $wgOut;
1228
1229 $td = $wgLang->timeanddate( $this->mTimestamp, true );
1230 $r = str_replace( "$1", "{$td}", wfMsg( "revisionasof" ) );
1231 $wgOut->setSubtitle( "({$r})" );
1232 }
1233
1234 # This function is called right before saving the wikitext,
1235 # so we can do things like signatures and links-in-context.
1236
1237 function preSaveTransform( $text )
1238 {
1239 $s = "";
1240 while ( "" != $text ) {
1241 $p = preg_split( "/<\\s*nowiki\\s*>/i", $text, 2 );
1242 $s .= $this->pstPass2( $p[0] );
1243
1244 if ( ( count( $p ) < 2 ) || ( "" == $p[1] ) ) { $text = ""; }
1245 else {
1246 $q = preg_split( "/<\\/\\s*nowiki\\s*>/i", $p[1], 2 );
1247 $s .= "<nowiki>{$q[0]}</nowiki>";
1248 $text = $q[1];
1249 }
1250 }
1251 return rtrim( $s );
1252 }
1253
1254 /* private */ function pstPass2( $text )
1255 {
1256 global $wgUser, $wgLang, $wgTitle, $wgLocaltimezone;
1257
1258 # Signatures
1259 #
1260 $n = $wgUser->getName();
1261 $k = $wgUser->getOption( "nickname" );
1262 if ( "" == $k ) { $k = $n; }
1263 if(isset($wgLocaltimezone)) {
1264 $oldtz = getenv("TZ"); putenv("TZ=$wgLocaltimezone");
1265 }
1266 /* Note: this is an ugly timezone hack for the European wikis */
1267 $d = $wgLang->timeanddate( date( "YmdHis" ), false ) .
1268 " (" . date( "T" ) . ")";
1269 if(isset($wgLocaltimezone)) putenv("TZ=$oldtz");
1270
1271 $text = preg_replace( "/~~~~/", "[[" . $wgLang->getNsText(
1272 Namespace::getUser() ) . ":$n|$k]] $d", $text );
1273 $text = preg_replace( "/~~~/", "[[" . $wgLang->getNsText(
1274 Namespace::getUser() ) . ":$n|$k]]", $text );
1275
1276 # Context links: [[|name]] and [[name (context)|]]
1277 #
1278 $tc = "[&;%\\-,.\\(\\)' _0-9A-Za-z\\/:\\x80-\\xff]";
1279 $np = "[&;%\\-,.' _0-9A-Za-z\\/:\\x80-\\xff]"; # No parens
1280 $conpat = "/^({$np}+) \\(({$tc}+)\\)$/";
1281
1282 $p1 = "/\[\[({$np}+) \\(({$np}+)\\)\\|]]/"; # [[page (context)|]]
1283 $p2 = "/\[\[\\|({$tc}+)]]/"; # [[|page]]
1284 $p3 = "/\[\[([A-Za-z _]+):({$np}+)\\|]]/"; # [[namespace:page|]]
1285 $p4 = "/\[\[([A-Aa-z _]+):({$np}+) \\(({$np}+)\\)\\|]]/";
1286 # [[ns:page (cont)|]]
1287 $context = "";
1288 $t = $wgTitle->getText();
1289 if ( preg_match( $conpat, $t, $m ) ) {
1290 $context = $m[2];
1291 }
1292 $text = preg_replace( $p4, "[[\\1:\\2 (\\3)|\\2]]", $text );
1293 $text = preg_replace( $p1, "[[\\1 (\\2)|\\1]]", $text );
1294 $text = preg_replace( $p3, "[[\\1:\\2|\\2]]", $text );
1295
1296 if ( "" == $context ) {
1297 $text = preg_replace( $p2, "[[\\1]]", $text );
1298 } else {
1299 $text = preg_replace( $p2, "[[\\1 ({$context})|\\1]]", $text );
1300 }
1301 # Replace local image links with new [[image:]] style
1302
1303 $text = preg_replace(
1304 "/(^|[^[])http:\/\/(www.|)wikipedia.com\/upload\/" .
1305 "([a-zA-Z0-9_:.~\%\-]+)\.(png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF)/",
1306 "\\1[[image:\\3.\\4]]", $text );
1307 $text = preg_replace(
1308 "/(^|[^[])http:\/\/(www.|)wikipedia.com\/images\/uploads\/" .
1309 "([a-zA-Z0-9_:.~\%\-]+)\.(png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF)/",
1310 "\\1[[image:\\3.\\4]]", $text );
1311
1312 return $text;
1313 }
1314
1315 /* Caching functions */
1316
1317 function tryFileCache() {
1318 global $wgTitle;
1319
1320 if($this->isFileCacheable()) {
1321 $cache = new CacheManager( $wgTitle );
1322 if($cache->isFileCacheGood( $this->mTouched )) {
1323 wfDebug( " tryFileCache() - about to load\n" );
1324 $cache->loadFromFileCache();
1325 exit;
1326 } else {
1327 wfDebug( " tryFileCache() - starting buffer\n" );
1328 if($cache->useGzip() && wfClientAcceptsGzip()) {
1329 /* For some reason, adding this header line over in
1330 CacheManager::saveToFileCache() fails on my test
1331 setup at home, though it works on the live install.
1332 Make double-sure... --brion */
1333 header( "Content-Encoding: gzip" );
1334 }
1335 ob_start( array(&$cache, 'saveToFileCache' ) );
1336 }
1337 } else {
1338 wfDebug( " tryFileCache() - not cacheable\n" );
1339 }
1340 }
1341
1342 function isFileCacheable() {
1343 global $wgUser, $wgTitle, $wgUseFileCache, $wgShowIPinHeader;
1344 global $action, $oldid, $diff, $redirect, $printable;
1345 return $wgUseFileCache
1346 and (!$wgShowIPinHeader)
1347 and ($wgUser->getId() == 0)
1348 and (!$wgUser->getNewtalk())
1349 and ($wgTitle->getNamespace != Namespace::getSpecial())
1350 and ($action == "view")
1351 and (!isset($oldid))
1352 and (!isset($diff))
1353 and (!isset($redirect))
1354 and (!isset($printable))
1355 and (!$this->mRedirectedFrom);
1356
1357 }
1358
1359
1360 }
1361
1362 ?>